#include "GameObjEnemy.h"
#include "kazmath\kazmath.h"


GameObjEnemy::GameObjEnemy(void)
{
	horiSpeed = 1.0f;
	vertSpeed = 0;
	jumpSpeed = 2.0f;
	rightMove = 0;
	leftMove = 0;
	upMove = 0;
	downMove = 0;
	jumpMove = 0;
	moveState = 0;
	gameObjType = ENEMY;
	isFlight = true;
}

GameObjEnemy::~GameObjEnemy(void)
{
}

void GameObjEnemy::updateTarget(GameObj* const target){
	if(target){
		if(!isFlight){
			CCPoint targetPos = target->sprite->getPosition();
			CCPoint pos = sprite->getPosition();
			if(targetPos.x < pos.x){
				leftMove = 1;
				rightMove = 0;
			}else if(targetPos.x > pos.x){
				rightMove = 1;
				leftMove = 0;
			}
		}else{
			CCPoint targetPos = target->sprite->getPosition();
			CCPoint pos = sprite->getPosition();
			targetPos.y = targetPos.y + 30;
			float dx = pos.x-targetPos.x;
			float dy = pos.y-targetPos.y;
			if(abs(dx+dy) < 2.0f)
				return;

			float deltaX = 0;
			float deltaY = 0;
			if(abs(dx) > 1.0f){
				deltaX = sqrt(pow((pos.x-targetPos.x), 2) * horiSpeed / (pow((pos.x-targetPos.x), 2) + pow((pos.y-targetPos.y), 2)));
			}
			if(abs(dy) > 1.0f){
				deltaY = sqrt(pow((pos.y-targetPos.y), 2) * horiSpeed / (pow((pos.x-targetPos.x), 2) + pow((pos.y-targetPos.y), 2)));
			}

			if(pos.x > targetPos.x){
				deltaX = deltaX * (-1);
			}
			if(pos.y > targetPos.y){
				deltaY = deltaY * (-1);
			}
			sprite->setPosition(ccp(pos.x + deltaX, pos.y + deltaY));
			float x = (pos.x-targetPos.x);
			float y = (pos.y-targetPos.y);
			float rot = atan(x / y) * 180.0f / 3.14f;
			if(x * y > 0 && x > 0 && y > 0){
				rot = rot + 180.0f;
			}
			if(x * y < 0 && x < 0 && x < 0){
				rot = rot + 180.0f;
			}
			sprite->setRotation(rot);
		}
	}
}

GameObjEnemy* GameObjEnemy::create(const char *pszFileName, const CCSize frameSize, const CCPoint &point, const float size, const int speed){
	
	GameObjEnemy *enemy = new GameObjEnemy();
	enemy->horiSpeed = speed;

	if(enemy){
		enemy->autorelease();
	}else{
		CC_SAFE_DELETE(enemy);
		return NULL;
	}

	// initial enemy's sprite.
	CCSprite *pobSprite = new CCSprite();
    if (pobSprite && pobSprite->initWithFile(pszFileName))
    {
		pobSprite->setAnchorPoint(ccp(0.5f, 0));
		pobSprite->setPosition(point);
        pobSprite->autorelease();
		enemy->sprite = pobSprite;
    }else{
		CC_SAFE_DELETE(pobSprite);
		CC_SAFE_DELETE(enemy);
		return NULL;
	}

	CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage(pszFileName);
				CCArray *splitAniframe = CCArray::createWithCapacity(18);
				for(int i = 0; i < 4; i++){
					CCSpriteFrame *frame = CCSpriteFrame::createWithTexture(texture, CCRectMake(i * frameSize.width, 0, frameSize.width, frameSize.height));
					splitAniframe->addObject(frame);
				}
				CCAnimation *splitAnimation = CCAnimation::createWithSpriteFrames(splitAniframe, 0.1f * size);
				CCAnimate *splitAnimate = CCAnimate::create(splitAnimation);
				enemy->sprite->runAction(CCRepeatForever::create(splitAnimate));

    return enemy;
}